package com.yunshang.shoppingmall.controller.Finace;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.Admin;
import com.yunshang.shoppingmall.entity.Finace.Finace;
import com.yunshang.shoppingmall.entity.FinaceToData;
import com.yunshang.shoppingmall.entity.FinaceTotal;
import com.yunshang.shoppingmall.entity.UserNo;
import com.yunshang.shoppingmall.service.Finace.FinaceService;
import com.yunshang.shoppingmall.service.Finace.FinaceTotalService;
import com.yunshang.shoppingmall.service.SysUserNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FinaceController {

    @Autowired
    FinaceService finaceService;

    @Autowired
    SysUserNoService sysUserNoService;
    @Autowired
    FinaceTotalService finaceTotalService;

    //财务列表页面
    @RequestMapping("/Finace/FinaceList")
    public String getFinaceList() {
        return "Finace/FinaceList";
    }

    @ResponseBody
    @RequestMapping("/Finace/JsonFinaceList")
    public String JsonFinaceList(int page, int limit, int calssify, String userNo) {
        PageHelper.startPage(page, limit);
        List<Finace> finaceList;


        if (calssify > 0) {
            if (userNo != null && !userNo.isEmpty()) {
                System.out.println("calssify:"+calssify+"userNo:"+userNo);
                finaceList = finaceService.selectFinaceListUserNo(calssify, userNo);
            } else {
                finaceList = finaceService.selectFinaceList(calssify);
            }
            PageInfo page1 = new PageInfo(finaceList);
            return layuiJson.layuiJsonList(finaceList, (int) page1.getTotal());
        }
        return layuiJson.layuiJsonList(null, 0);
    }

    @ResponseBody
    @RequestMapping("/Finace/getUserBalance")
    public String getUserBalance() {
        //double balance = finaceService.selectByPrimaryUserId(1);
        return ("---->" + "fdsf");
    }

    //会员编号充值
    @RequestMapping("/Finace/getFinaceAdd")
    public ModelAndView getUserNoAdd() {
        ModelAndView modelAndView = new ModelAndView("Finace/FinaceAdd");
        return modelAndView;
    }

    //会员编号--充值
    @ResponseBody
    @RequestMapping("/Finace/FinaceAdd")
    @Transactional
    public Map<String, Object> FinaceAdd(HttpServletRequest request, @RequestParam("userId") String userId,
                                         @RequestParam("amount") BigDecimal amount,
                                         @RequestParam("calssify") int calssify,
                                         @RequestParam("remark") String remark) {
        System.out.println("yonghu:" + userId);
        Map<String, Object> map = new HashMap<>();
        //查询充值用户是否存在
        UserNo userNo = null;
        int userIds = 0;
        if (userId != null && userId != "") {
            userIds = Integer.parseInt(userId);
            userNo = sysUserNoService.selectByPrimaryKey(userIds);
            System.out.println("userNo=====" + userNo);
            if (userNo == null) {
                map.put("result", "用户不存在！");
                return map;
            }
        }
        int AID = 0;
         AID = (int)request.getSession().getAttribute("adminId");
        if(AID==0){
            map.put("result","请后台管理员登录！");
            return map;
        }

            //充值金额
            double amounts = 0.00;
            if (amount != null) {
                amounts = amount.doubleValue();
            }
            FinaceToData finaceToData = null;

            //充值
            FinaceTotal finaceTotal2 = finaceTotalService.selectFinaceTotal(userIds, calssify+"");

            if (finaceTotal2 == null) {//用户第一次充值
                FinaceTotal finaceTotal3 = new FinaceTotal();
                finaceTotal3.setBalance(amounts);
                finaceTotal3.setUserId(userIds);
                finaceTotal3.setfType(calssify+"");
                finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                finaceTotal3.setOpratorId(AID);
                finaceTotal3.setAmount(amounts);
                finaceTotal3.setOpratorUId(userIds);
                int firstRow = finaceTotalService.insertSelective(finaceTotal3);

                //积分表中记录明细
                finaceToData = new FinaceToData();
                finaceToData.setClassify(calssify);
                finaceToData.setAmount(amount);
                finaceToData.setUserId(userIds);
                finaceToData.setBalance(new BigDecimal(amounts));
                finaceToData.setFinaceTime(new Timestamp(System.currentTimeMillis()));
                finaceToData.setFinaceType("777");
                finaceToData.setOpratorUId(userIds);
                finaceToData.setOpratorId(AID);
                finaceToData.setRemark(remark);
                int row1 = finaceService.insertFinaceData(finaceToData);

                if(firstRow>0&&row1>0){
                    map.put("result","充值成功！");
                }else{
                    map.put("result","充值失败！");
                }
                return map;
            }

            double balance2 = finaceTotal2.getBalance();//用户的余额
            int fId = finaceTotal2.getfId();

            FinaceTotal finaceTotal = new FinaceTotal();
            finaceTotal.setUserId(userIds);
            finaceTotal.setfType(calssify+"");
            finaceTotal.setBalance(balance2 + amounts);
            finaceTotal.setfId(fId);
            int row = finaceTotalService.updateByPrimaryKeySelective(finaceTotal);

            if (row > 0) {//充值成功
                finaceToData = new FinaceToData();
                finaceToData.setClassify(calssify);
                finaceToData.setAmount(amount);
                finaceToData.setUserId(userIds);
                finaceToData.setBalance(new BigDecimal(balance2 + amounts));
                finaceToData.setFinaceTime(new Timestamp(System.currentTimeMillis()));
                finaceToData.setFinaceType("777");
                finaceToData.setOpratorUId(userIds);
                finaceToData.setOpratorId(AID);
                finaceToData.setRemark(remark);

            }
            int row1 = finaceService.insertFinaceData(finaceToData);
            if (row > 0 && row1 > 0) {
                map.put("result", "充值成功！");
            } else {
                map.put("result", "充值失败！");
            }

        return map;
    }

    @ResponseBody
    @RequestMapping("/user/JsonUserNo2")
    public UserNo selectUserNoByUserNo(@RequestParam("userNo") String userNo) {
        UserNo userNo1 = null;
        if (userNo != null && userNo != "") {
            userNo1 = sysUserNoService.selectUserNoByUserNo(userNo);
            if (userNo1 != null) {

                return userNo1;
            }
            return null;

        } else {
            return null;
        }

    }

    @ResponseBody
    @RequestMapping("/selectUserNoByUserNoId")
    public UserNo selectUserNoByUserNoId(@RequestParam("userNoId") String userNoId) {
        int userNoIds = 0;
        if (userNoId != null && userNoId != "") {
            userNoIds = Integer.parseInt(userNoId);
        } else {
            return null;
        }
        UserNo userNo = sysUserNoService.selectByPrimaryKey(userNoIds);
        System.out.println("---------->" + userNo.getUserNo());
        if (userNo != null) {
            return userNo;
        } else {
            return null;
        }
    }


}
